package com.irdstudio.apicenter.gateway.core.appsession;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.irdstudio.apicenter.gateway.core.jdbc.MaccsBaseDAO;
import com.irdstudio.apicenter.gateway.util.pub.KeyUtil;
import com.irdstudio.apicenter.gateway.util.spring.BeanUtils;

/**
 * 处理会话的工具类
 * @author fmf
 *
 */
public class AppSessionUtil {
	/**
	 * 创建会话session
	 * @param appSession 会话对象
	 * @return
	 * @throws Exception 
	 */
	public static boolean createSession( AppSession appSession) throws Exception{
		// 1.生成新的会话id
        String inputTime=String.valueOf( new Date().getTime());
		
		MaccsBaseDAO jdbcDao = BeanUtils.getBean(MaccsBaseDAO.class);
		AppSession session=jdbcDao.findBean(AppSession.class,new Object[] { appSession.getDeviceId() });
		//2.不存在会话id创建会话id
		if(session == null){
			String sql="insert into app_session (app_session_id,token,input_time,last_token,client_ip,device_id,device_type,channel_type,address) values (?,?,?,?,?,?,?,?,?)";
			int code=jdbcDao.insert(sql, new Object[]{appSession.getDeviceId(),appSession.getAddress(),appSession.getInputTime(),appSession.getDeviceId(),appSession.getClientIp(),appSession.getDeviceId(),appSession.getDeviceType(),appSession.getChannelType(),appSession.getAddress()});
			if(code != 1){
				throw new Exception();
			}
			return true;
		}else{
			//更新会话
		}
		
		return false;
	}
	/**
	 * 检查会话,会话检查成功自动更新时间
	 * @param appSessionId
	 * @return
	 */
	public static boolean checkSession(String  appSessionId ){
		return false;
	}
	

}
